package cn.shuto.liferay.plugin.chart.service.model;

import cn.shuto.liferay.plugin.chart.service.service.ClpSerializer;
import cn.shuto.liferay.plugin.chart.service.service.OptionLocalServiceUtil;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.lar.StagedModelType;
import com.liferay.portal.kernel.util.DateUtil;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;
import com.liferay.portal.util.PortalUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class OptionClp extends BaseModelImpl<Option> implements Option {
    private String _uuid;
    private long _optionId;
    private long _groupId;
    private long _companyId;
    private long _userId;
    private String _userUuid;
    private String _userName;
    private Date _createDate;
    private Date _modifiedDate;
    private String _name;
    private String _description;
    private String _templateParamValue;
    private long _chartTemplateId;
    private long _dataSourceId;
    private String _dataSql;
    private BaseModel<?> _optionRemoteModel;
    private Class<?> _clpSerializerClass = cn.shuto.liferay.plugin.chart.service.service.ClpSerializer.class;

    public OptionClp() {
    }

    @Override
    public Class<?> getModelClass() {
        return Option.class;
    }

    @Override
    public String getModelClassName() {
        return Option.class.getName();
    }

    @Override
    public long getPrimaryKey() {
        return _optionId;
    }

    @Override
    public void setPrimaryKey(long primaryKey) {
        setOptionId(primaryKey);
    }

    @Override
    public Serializable getPrimaryKeyObj() {
        return _optionId;
    }

    @Override
    public void setPrimaryKeyObj(Serializable primaryKeyObj) {
        setPrimaryKey(((Long) primaryKeyObj).longValue());
    }

    @Override
    public Map<String, Object> getModelAttributes() {
        Map<String, Object> attributes = new HashMap<String, Object>();

        attributes.put("uuid", getUuid());
        attributes.put("optionId", getOptionId());
        attributes.put("groupId", getGroupId());
        attributes.put("companyId", getCompanyId());
        attributes.put("userId", getUserId());
        attributes.put("userName", getUserName());
        attributes.put("createDate", getCreateDate());
        attributes.put("modifiedDate", getModifiedDate());
        attributes.put("name", getName());
        attributes.put("description", getDescription());
        attributes.put("templateParamValue", getTemplateParamValue());
        attributes.put("chartTemplateId", getChartTemplateId());
        attributes.put("dataSourceId", getDataSourceId());
        attributes.put("dataSql", getDataSql());

        return attributes;
    }

    @Override
    public void setModelAttributes(Map<String, Object> attributes) {
        String uuid = (String) attributes.get("uuid");

        if (uuid != null) {
            setUuid(uuid);
        }

        Long optionId = (Long) attributes.get("optionId");

        if (optionId != null) {
            setOptionId(optionId);
        }

        Long groupId = (Long) attributes.get("groupId");

        if (groupId != null) {
            setGroupId(groupId);
        }

        Long companyId = (Long) attributes.get("companyId");

        if (companyId != null) {
            setCompanyId(companyId);
        }

        Long userId = (Long) attributes.get("userId");

        if (userId != null) {
            setUserId(userId);
        }

        String userName = (String) attributes.get("userName");

        if (userName != null) {
            setUserName(userName);
        }

        Date createDate = (Date) attributes.get("createDate");

        if (createDate != null) {
            setCreateDate(createDate);
        }

        Date modifiedDate = (Date) attributes.get("modifiedDate");

        if (modifiedDate != null) {
            setModifiedDate(modifiedDate);
        }

        String name = (String) attributes.get("name");

        if (name != null) {
            setName(name);
        }

        String description = (String) attributes.get("description");

        if (description != null) {
            setDescription(description);
        }

        String templateParamValue = (String) attributes.get(
                "templateParamValue");

        if (templateParamValue != null) {
            setTemplateParamValue(templateParamValue);
        }

        Long chartTemplateId = (Long) attributes.get("chartTemplateId");

        if (chartTemplateId != null) {
            setChartTemplateId(chartTemplateId);
        }

        Long dataSourceId = (Long) attributes.get("dataSourceId");

        if (dataSourceId != null) {
            setDataSourceId(dataSourceId);
        }

        String dataSql = (String) attributes.get("dataSql");

        if (dataSql != null) {
            setDataSql(dataSql);
        }
    }

    @Override
    public String getUuid() {
        return _uuid;
    }

    @Override
    public void setUuid(String uuid) {
        _uuid = uuid;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setUuid", String.class);

                method.invoke(_optionRemoteModel, uuid);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getOptionId() {
        return _optionId;
    }

    @Override
    public void setOptionId(long optionId) {
        _optionId = optionId;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setOptionId", long.class);

                method.invoke(_optionRemoteModel, optionId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getGroupId() {
        return _groupId;
    }

    @Override
    public void setGroupId(long groupId) {
        _groupId = groupId;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setGroupId", long.class);

                method.invoke(_optionRemoteModel, groupId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getCompanyId() {
        return _companyId;
    }

    @Override
    public void setCompanyId(long companyId) {
        _companyId = companyId;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setCompanyId", long.class);

                method.invoke(_optionRemoteModel, companyId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getUserId() {
        return _userId;
    }

    @Override
    public void setUserId(long userId) {
        _userId = userId;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setUserId", long.class);

                method.invoke(_optionRemoteModel, userId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getUserUuid() throws SystemException {
        return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
    }

    @Override
    public void setUserUuid(String userUuid) {
        _userUuid = userUuid;
    }

    @Override
    public String getUserName() {
        return _userName;
    }

    @Override
    public void setUserName(String userName) {
        _userName = userName;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setUserName", String.class);

                method.invoke(_optionRemoteModel, userName);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public Date getCreateDate() {
        return _createDate;
    }

    @Override
    public void setCreateDate(Date createDate) {
        _createDate = createDate;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setCreateDate", Date.class);

                method.invoke(_optionRemoteModel, createDate);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public Date getModifiedDate() {
        return _modifiedDate;
    }

    @Override
    public void setModifiedDate(Date modifiedDate) {
        _modifiedDate = modifiedDate;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setModifiedDate", Date.class);

                method.invoke(_optionRemoteModel, modifiedDate);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getName() {
        return _name;
    }

    @Override
    public void setName(String name) {
        _name = name;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setName", String.class);

                method.invoke(_optionRemoteModel, name);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getDescription() {
        return _description;
    }

    @Override
    public void setDescription(String description) {
        _description = description;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setDescription", String.class);

                method.invoke(_optionRemoteModel, description);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getTemplateParamValue() {
        return _templateParamValue;
    }

    @Override
    public void setTemplateParamValue(String templateParamValue) {
        _templateParamValue = templateParamValue;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setTemplateParamValue",
                        String.class);

                method.invoke(_optionRemoteModel, templateParamValue);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getChartTemplateId() {
        return _chartTemplateId;
    }

    @Override
    public void setChartTemplateId(long chartTemplateId) {
        _chartTemplateId = chartTemplateId;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setChartTemplateId", long.class);

                method.invoke(_optionRemoteModel, chartTemplateId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getDataSourceId() {
        return _dataSourceId;
    }

    @Override
    public void setDataSourceId(long dataSourceId) {
        _dataSourceId = dataSourceId;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setDataSourceId", long.class);

                method.invoke(_optionRemoteModel, dataSourceId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getDataSql() {
        return _dataSql;
    }

    @Override
    public void setDataSql(String dataSql) {
        _dataSql = dataSql;

        if (_optionRemoteModel != null) {
            try {
                Class<?> clazz = _optionRemoteModel.getClass();

                Method method = clazz.getMethod("setDataSql", String.class);

                method.invoke(_optionRemoteModel, dataSql);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public StagedModelType getStagedModelType() {
        return new StagedModelType(PortalUtil.getClassNameId(
                Option.class.getName()));
    }

    public BaseModel<?> getOptionRemoteModel() {
        return _optionRemoteModel;
    }

    public void setOptionRemoteModel(BaseModel<?> optionRemoteModel) {
        _optionRemoteModel = optionRemoteModel;
    }

    public Object invokeOnRemoteModel(String methodName,
        Class<?>[] parameterTypes, Object[] parameterValues)
        throws Exception {
        Object[] remoteParameterValues = new Object[parameterValues.length];

        for (int i = 0; i < parameterValues.length; i++) {
            if (parameterValues[i] != null) {
                remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
            }
        }

        Class<?> remoteModelClass = _optionRemoteModel.getClass();

        ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

        Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

        for (int i = 0; i < parameterTypes.length; i++) {
            if (parameterTypes[i].isPrimitive()) {
                remoteParameterTypes[i] = parameterTypes[i];
            } else {
                String parameterTypeName = parameterTypes[i].getName();

                remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
            }
        }

        Method method = remoteModelClass.getMethod(methodName,
                remoteParameterTypes);

        Object returnValue = method.invoke(_optionRemoteModel,
                remoteParameterValues);

        if (returnValue != null) {
            returnValue = ClpSerializer.translateOutput(returnValue);
        }

        return returnValue;
    }

    @Override
    public void persist() throws SystemException {
        if (this.isNew()) {
            OptionLocalServiceUtil.addOption(this);
        } else {
            OptionLocalServiceUtil.updateOption(this);
        }
    }

    @Override
    public Option toEscapedModel() {
        return (Option) ProxyUtil.newProxyInstance(Option.class.getClassLoader(),
            new Class[] { Option.class }, new AutoEscapeBeanHandler(this));
    }

    @Override
    public Object clone() {
        OptionClp clone = new OptionClp();

        clone.setUuid(getUuid());
        clone.setOptionId(getOptionId());
        clone.setGroupId(getGroupId());
        clone.setCompanyId(getCompanyId());
        clone.setUserId(getUserId());
        clone.setUserName(getUserName());
        clone.setCreateDate(getCreateDate());
        clone.setModifiedDate(getModifiedDate());
        clone.setName(getName());
        clone.setDescription(getDescription());
        clone.setTemplateParamValue(getTemplateParamValue());
        clone.setChartTemplateId(getChartTemplateId());
        clone.setDataSourceId(getDataSourceId());
        clone.setDataSql(getDataSql());

        return clone;
    }

    @Override
    public int compareTo(Option option) {
        int value = 0;

        value = DateUtil.compareTo(getCreateDate(), option.getCreateDate());

        value = value * -1;

        if (value != 0) {
            return value;
        }

        return 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof OptionClp)) {
            return false;
        }

        OptionClp option = (OptionClp) obj;

        long primaryKey = option.getPrimaryKey();

        if (getPrimaryKey() == primaryKey) {
            return true;
        } else {
            return false;
        }
    }

    public Class<?> getClpSerializerClass() {
        return _clpSerializerClass;
    }

    @Override
    public int hashCode() {
        return (int) getPrimaryKey();
    }

    @Override
    public String toString() {
        StringBundler sb = new StringBundler(29);

        sb.append("{uuid=");
        sb.append(getUuid());
        sb.append(", optionId=");
        sb.append(getOptionId());
        sb.append(", groupId=");
        sb.append(getGroupId());
        sb.append(", companyId=");
        sb.append(getCompanyId());
        sb.append(", userId=");
        sb.append(getUserId());
        sb.append(", userName=");
        sb.append(getUserName());
        sb.append(", createDate=");
        sb.append(getCreateDate());
        sb.append(", modifiedDate=");
        sb.append(getModifiedDate());
        sb.append(", name=");
        sb.append(getName());
        sb.append(", description=");
        sb.append(getDescription());
        sb.append(", templateParamValue=");
        sb.append(getTemplateParamValue());
        sb.append(", chartTemplateId=");
        sb.append(getChartTemplateId());
        sb.append(", dataSourceId=");
        sb.append(getDataSourceId());
        sb.append(", dataSql=");
        sb.append(getDataSql());
        sb.append("}");

        return sb.toString();
    }

    @Override
    public String toXmlString() {
        StringBundler sb = new StringBundler(46);

        sb.append("<model><model-name>");
        sb.append("cn.shuto.liferay.plugin.chart.service.model.Option");
        sb.append("</model-name>");

        sb.append(
            "<column><column-name>uuid</column-name><column-value><![CDATA[");
        sb.append(getUuid());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>optionId</column-name><column-value><![CDATA[");
        sb.append(getOptionId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>groupId</column-name><column-value><![CDATA[");
        sb.append(getGroupId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>companyId</column-name><column-value><![CDATA[");
        sb.append(getCompanyId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>userId</column-name><column-value><![CDATA[");
        sb.append(getUserId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>userName</column-name><column-value><![CDATA[");
        sb.append(getUserName());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>createDate</column-name><column-value><![CDATA[");
        sb.append(getCreateDate());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>modifiedDate</column-name><column-value><![CDATA[");
        sb.append(getModifiedDate());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>name</column-name><column-value><![CDATA[");
        sb.append(getName());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>description</column-name><column-value><![CDATA[");
        sb.append(getDescription());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>templateParamValue</column-name><column-value><![CDATA[");
        sb.append(getTemplateParamValue());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>chartTemplateId</column-name><column-value><![CDATA[");
        sb.append(getChartTemplateId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>dataSourceId</column-name><column-value><![CDATA[");
        sb.append(getDataSourceId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>dataSql</column-name><column-value><![CDATA[");
        sb.append(getDataSql());
        sb.append("]]></column-value></column>");

        sb.append("</model>");

        return sb.toString();
    }
}
